//
//  SourceDetailHeaderView.swift
//  test
//
//  Created by colin on 2019/3/29.
//  Copyright © 2019 colin. All rights reserved.
//

import UIKit

class SourceDetailHeaderView: UIView {
    
    var contentHeight:CGFloat = 0
    var viewHeight:CGFloat = 0
    
    var segments:CLSegmentedView?
    
    private lazy var sourceType = SourceType.精讲
    private lazy var imageViewPhoto = UIImageView()
    private lazy var labelName = UILabel()
    private lazy var labelAuthor = UILabel()
    private lazy var labelBuyedCount = UILabel()
    
    convenience init(sourceType:SourceType, data:JSONObject?) {
        self.init()
        self.backgroundColor = UIColor.white
        self.sourceType = sourceType
        
        self.do {
            // 封面
            self.imageViewPhoto.do {
                $0.layer.masksToBounds = true
                $0.layer.cornerRadius = 3.0
            }
            $0.addSubview(self.imageViewPhoto)
            self.imageViewPhoto.snp.makeConstraints({ (make) in
                make.left.right.top.equalToSuperview()
                let ratio:CGFloat = 280.0 / 375.0
                make.height.equalTo(UIScreen.main.bounds.width * ratio)
            })
            
            // 名称
            self.labelName.do {
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 17.0, weight: .bold)
                $0.numberOfLines = 0
            }
            $0.addSubview(self.labelName)
            self.labelName.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(15.0)
                make.right.equalToSuperview().offset(-15.0)
                make.top.equalTo(self.imageViewPhoto.snp.bottom).offset(20.0)
            })
            
            // 作者
            self.labelAuthor.do {
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 12.0, weight: .light)
                $0.setContentHuggingPriority(.defaultLow, for: .horizontal)
                $0.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
            }
            $0.addSubview(self.labelAuthor)
            self.labelAuthor.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(15.0)
                make.top.equalTo(self.labelName.snp.bottom).offset(30.0)
            })
            
            if sourceType != .专题 {
                //已购人数
                let labelBuyedTag = UILabel().then {
                    $0.text = "人已购"
                    $0.textColor = UIColor(hex: "343434")
                    $0.font = UIFont.systemFont(ofSize: 11.0, weight: .light)
                    $0.setContentHuggingPriority(.required, for: .horizontal)
                    $0.setContentCompressionResistancePriority(.required, for: .horizontal)
                }
                $0.addSubview(labelBuyedTag)
                labelBuyedTag.snp.makeConstraints({ (make) in
                    make.right.equalToSuperview().offset(-15.0)
                    make.bottom.equalTo(self.labelAuthor.snp.bottom)
                })
                
                self.labelBuyedCount.do {
                    $0.textColor = UIColor(hex: "343434")
                    $0.font = UIFont.systemFont(ofSize: 20.0, weight: .regular)
                    $0.setContentHuggingPriority(.required, for: .horizontal)
                    $0.setContentCompressionResistancePriority(.required, for: .horizontal)
                }
                $0.addSubview(self.labelBuyedCount)
                self.labelBuyedCount.snp.makeConstraints({ (make) in
                    make.left.equalTo(self.labelAuthor.snp.right).offset(15.0)
                    make.right.equalTo(labelBuyedTag.snp.left).offset(-3.0)
                    make.bottom.equalTo(self.labelAuthor.snp.bottom).offset(2.0)
                })
            }
            
            switch sourceType {
            case .专题,.有声书,.电子书,.精讲:
                let segments_ = CLSegmentedView(segments: ["目录","详情"])
                $0.addSubview(segments_)
                segments_.snp.makeConstraints({ (make) in
                    make.left.right.bottom.equalToSuperview()
                    make.height.equalTo(44.0)
                })
                self.segments = segments_
            default:break
            }
            
            // 底部分割线
            let line = UIView()
            line.backgroundColor = UIColor(hex: "EFEFEF")
            self.addSubview(line)
            line.snp.makeConstraints { (make) in
                make.left.right.equalToSuperview()
                make.height.equalTo(10.0)
                if let segments_ = self.segments {
                    make.bottom.equalTo(segments_.snp.top)
                } else {
                    make.bottom.equalToSuperview()
                }
            }
            
        }
        
        // 封面
        if let cover_url = data?["cover_url"] as? String,!cover_url.isEmpty,
            let baseUrl = URL(string: Router.baseUrl) {
            let imageUrl = baseUrl.appendingPathComponent(cover_url)
            self.imageViewPhoto.kf.setImage(with: imageUrl)
        } else {
            self.imageViewPhoto.image = nil
        }
        
        self.labelName.text = data?["title"] as? String
        self.labelAuthor.text = data?["author"] as? String
        
        if sourceType != .专题 {
            if let trade_count = data?["trade_count"] as? Int {
                self.labelBuyedCount.text = "\(trade_count)"
            } else {
                self.labelBuyedCount.text = "0"
            }
        }
        
        // 计算高度
        self.viewHeight = 0
        
        // 图片高度
        let ratio:CGFloat = 280.0 / 375.0
        self.viewHeight += UIScreen.main.bounds.width * ratio
        
        // 标题
        self.viewHeight += 20.0
        
        if let name = data?["title"] as? String {
            self.viewHeight += name.size(font: UIFont.systemFont(ofSize: 17.0, weight: .bold), containerWidth: UIScreen.main.bounds.width - 30.0).height
        }
        
        // 作者
        self.viewHeight += 15.0
        self.viewHeight += UIFont.systemFont(ofSize: 12.0, weight: .light).lineHeight
        self.viewHeight += 15.0
        
        self.viewHeight += 10.0
        
        switch sourceType {
        case .图文,.音频,.视频:
            self.contentHeight = self.viewHeight
        default:
            self.contentHeight = self.viewHeight
            self.viewHeight += 44.0
        }
        
    }
}
